package com.lei.design.command;

/**
 * @Author LeiXiaoLei
 * @Date 2022/5/20 10:52
 * @Version 1.0
 */
public class RemoteControl {
    private final Command[] onCommand;
    private final Command[] offCommand;
    private Command undoCommand;

    public RemoteControl() {
        int commandSize = Constant.COMMAND_SIZE;
        onCommand = new Command[commandSize];
        offCommand = new Command[commandSize];
        NoCommand noCommand = new NoCommand();
        for (int i = 0; i < commandSize; i++) {
            onCommand[i] = noCommand;
            offCommand[i] = noCommand;
        }
        undoCommand = noCommand;
    }

    public void setCommand(int slot, Command onCommand, Command offCommand) {
        this.onCommand[slot] = onCommand;
        this.offCommand[slot] = offCommand;
    }

    public void OnButtonWasPushed(int slot) {
        this.undoCommand = this.onCommand[slot];
        this.onCommand[slot].execute();
    }

    public void offButtonWasPushed(int slot) {
        this.undoCommand = this.offCommand[slot];
        this.offCommand[slot].execute();
    }

    public void undoButtonWasPushed() {
        this.undoCommand.undo();
    }
}
